﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Encoder=System.Drawing.Imaging.Encoder;
using Size=System.Drawing.Size;


namespace ViewModel
{
    public class GestionImage
    {
        private static Size _size = new Size(100,100);

        public static ImageSource Preview(Uri uri)
        {
            BitmapImage image = new BitmapImage(uri);
            int widthImg = (int)image.Width;
            int heightImg = (int)image.Height;
            float rPercentW = 0, rPercentH = 0;

            rPercentW = ((float)_size.Width / (float)widthImg);
            rPercentH = ((float)_size.Height / (float)heightImg);

            BitmapImage imageResized = new BitmapImage();
            imageResized.BeginInit();
            imageResized.UriSource = uri;
            //we keep the picture at the same format => keep the hightest % of reduction.
            if (rPercentH < rPercentW) imageResized.DecodePixelHeight = _size.Height; else imageResized.DecodePixelWidth = _size.Width;

            imageResized.EndInit();
            return imageResized;
        }

        /// <summary>
        /// resize an image thanks to size in param.
        /// TODO: reziseimg(typeresize["stretch" || "crop" || "keepFormat"])
        /// </summary>
        /// <param name="img"></param>
        /// <param name="size"></param>
        /// <returns></returns>
        public static Image ResizeImage(Image img)
        {
            int widthImg = img.Width;
            int heightImg = img.Height;

            float rPercent = 0, rPercentW = 0, rPercentH = 0;

            rPercentW = ((float)_size.Width / (float)widthImg);
            rPercentH = ((float)_size.Height / (float)heightImg);

            //we keep the picture at the same format => keep the hightest % of reduction.
            rPercent = (rPercentH < rPercentW) ? rPercentH : rPercentW;

            int destWidth = (int)(widthImg * rPercent);
            int destHeight = (int)(heightImg * rPercent);

            Bitmap retPic = new Bitmap(destWidth, destHeight);
            Graphics g = Graphics.FromImage((Image)retPic);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;

            g.DrawImage(img, 0, 0, destWidth, destHeight);
            g.Dispose();

            return (Image)retPic;
        }

        public static ImageSource ImageToImageSource(System.Drawing.Image image)
        {
            BitmapImage imgSource = new BitmapImage();
            imgSource.BeginInit();
            imgSource.StreamSource = new MemoryStream(ConvertImageToByteArray(image));
            imgSource.EndInit();
            return imgSource;
        }

        private static byte[] ConvertImageToByteArray(Image img)
        {
            MemoryStream ms = new MemoryStream();
            img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);

            return ms.ToArray();
        }
    }
}
